package com.example.demo.controller;

import com.example.demo.api.CommonResult;
import com.example.demo.api.ResultCode;
import com.example.demo.entity.Info;
import com.example.demo.entity.Report;
import com.example.demo.service.InfoService;
import com.example.demo.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.SplittableRandom;


@RestController
@RequestMapping("/house/v1")
public class InfoController {

    @Autowired
    private InfoService infoservice;

    @Autowired
    private TokenService tokenservice;

    /**
     * 发布信息
     */
    @PostMapping("/myinfo/{username}/{token}")
    public CommonResult postInfo(@RequestBody Info info,
                                 @PathVariable(name="token") String token,
                                 @PathVariable(name="username") String username) {
        System.out.println("......发布信息......");
        System.out.println(info.toString());
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                infoservice.insertAInfo(info);
                return CommonResult.result(ResultCode.POST_INFO_SUCCESS.getCode(), ResultCode.POST_INFO_SUCCESS.getMessage());
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.POST_INFO_FAILED.getCode(), ResultCode.POST_INFO_FAILED.getMessage());
        }
    }

    /**
     * 查看自己发布的出售信息
     */
    @GetMapping("/myinfo/{username}/{token}")
    public CommonResult lookpostInfo(@PathVariable(name="token") String token,
                                     @PathVariable(name="username") String username) {
        System.out.println("......查看自己发布的出售信息......");
        List<Info> info;
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                info = infoservice.findAllInfo_Username(username);
                /** 发布成功 **/
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), info);
            } else {
                /** 非法用户 **/
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            /** 发布失败 **/
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 删除信息
     */
    @DeleteMapping("/myinfo/{username}/{token}/{id}")
    public CommonResult deleteInfo(@PathVariable(name="id") String id,
                                 @PathVariable(name="token") String token,
                                 @PathVariable(name="username") String username) {
        System.out.println("......删除信息......");
        System.out.println(token);
        System.out.println(username);
        try {
            if (tokenservice.searchToken(token) && username.equals(tokenservice.getName(token))) {
                int _id = Integer.valueOf(id);
                if(infoservice.findUserByInfoid(_id).equals(username)) {
                    infoservice.deleteAInfo(_id);
                    return CommonResult.result(ResultCode.DELETE_INFO_SUCCESS.getCode(), ResultCode.DELETE_INFO_SUCCESS.getMessage());
                } else{
                    return CommonResult.result(ResultCode.ADMIN_FAILED.getCode(), ResultCode.ADMIN_FAILED.getMessage());
                }
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.DELETE_INFO_FAILED.getCode(), ResultCode.DELETE_INFO_FAILED.getMessage());
        }
    }

    /**
     * 修改信息(放弃)
     */
//    @PutMapping("/myinfo/{username}/{token}")
//    public CommonResult putInfo(@RequestBody Info info,
//                                 @PathVariable(name="token") String token,
//                                 @PathVariable(name="username") String username) {
//        System.out.println("......修改信息......");
//        try {
//            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
//                System.out.println(info.getId());
//                infoservice.updateAInfo(info);
//                /**发布出售成功**/
//                return CommonResult.result(ResultCode.PUT_INFO_SUCCESS.getCode(), ResultCode.PUT_INFO_SUCCESS.getMessage());
//            } else {
//                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
//            }
//        } catch (Exception e) {
//            System.out.println(e);
//            return CommonResult.result(ResultCode.PUT_INFO_FAILED.getCode(), ResultCode.PUT_INFO_FAILED.getMessage());
//        }
//    }

    /**
     * 浏览首页的信息
     */
    @GetMapping("/otherinfo/{username}/{token}")
    public CommonResult browse ( @PathVariable(name="token") String token,
                                 @PathVariable(name="username") String username) {
        System.out.println("......浏览信息......");
        List<Info> info;
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                info = infoservice.findAllInfo();
                System.out.println(info.toString());
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), info);
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 查看单个信息
     */
    @GetMapping("/info/{username}/{token}/{id}")
    public CommonResult lookAInfo ( @PathVariable(name="id") int id,
                                 @PathVariable(name="token") String token,
                                 @PathVariable(name="username") String username) {
        System.out.println("......查看单个信息......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                Info info = infoservice.findAInfo(id);
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), info);
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 举报信息
     */
    @PostMapping("/reportinfo/{username}/{token}/{id}")
    public CommonResult report (@PathVariable String  id,
                                @RequestBody Report report,
                                @PathVariable(name="token") String token,
                                @PathVariable(name="username") String username) {
        System.out.println("......举报信息......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                int _id = Integer.valueOf(id);
                report.setInfo_id(_id);
                System.out.println(report.toString());
                infoservice.insertAReport(report);
                return CommonResult.result(ResultCode.REPORT_SUCCESS.getCode(), ResultCode.REPORT_SUCCESS.getMessage());
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.REPORT_SUCCESS.getCode(), ResultCode.REPORT_FAILED.getMessage());
        }
    }

    /**
     * 搜索title
     */
    @PostMapping("/info1/{username}/{token}")
    public CommonResult matchTitle (@PathVariable(name="token") String token,
                                    @PathVariable(name="username") String username,
                                    @RequestBody Info info){
        System.out.println(info.getTitle());
        System.out.println("......搜索信息......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                System.out.println(info.getTitle());
                List<Info> list = infoservice.matchTitle(info.getTitle());
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), list);
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }

    }

}
